package com.project.datacenter.schedule;


import com.project.datacenter.dto.BusLineStationDto;
import com.project.datacenter.dto.ParamDto;
import com.project.datacenter.dto.UserXlDto;
import com.project.datacenter.entity.*;
import com.project.datacenter.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * - 定时任务
 */
@Slf4j
@Component
@EnableScheduling
public class Scheduler {

    @Resource
    BusLineService busLineService;
    @Resource
    BusLineStationService busLineStationService;
    @Resource
    BusVehicleService busVehicleService;
    @Resource
    BusDirectionService busDirectionService;
    @Resource
    EnterpriseService enterpriseService;
    @Resource
    UserXlService userXlService;

//    @Scheduled(cron = "0 5 1 * * ?")
    private void syncJob() {
        log.info("每天1点5分定时任务执行开始");
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate now = LocalDate.now();
            LocalDate before = now.minusDays(1);
            LocalDate LastMonth = now.minusDays(30);
            ParamDto dto = new ParamDto();
            dto.setDate(formatter.format(before));
            ParamDto dto2 = new ParamDto();
            dto2.setDate(formatter.format(LastMonth));

            log.info("【企业基本信息】" + "【" + formatter.format(before) + "】");
            List<TobjEnterprise> tobjEnterpriseList = enterpriseService.getList();
            if (tobjEnterpriseList.size() > 0) {
                enterpriseService.deleteByDate(dto);
                for (TobjEnterprise tobjEnterprise : tobjEnterpriseList) {
                    tobjEnterprise.setRq(dto.getDate());
                }
                enterpriseService.saveList(tobjEnterpriseList);
                List<TobjEnterprise> list = tobjEnterpriseList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
                if (list.size() > 0) {
                    enterpriseService.deleteAll();
                    enterpriseService.insert(list);
                }
            }

            log.info("【线路基础信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusLine> tobjBusLineList = busLineService.getList();
            if (tobjBusLineList.size() > 0) {
                busLineService.deleteByDate(dto);
                for (TobjBusLine tobjBusLine : tobjBusLineList) {
                    tobjBusLine.setRq(dto.getDate());
                }
                busLineService.saveList(tobjBusLineList);
            }
            log.info("【线路走向信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusDirection> tobjBusDirectionList = busDirectionService.getList();
            if (tobjBusDirectionList.size() > 0) {
                busDirectionService.deleteByDate(dto);
                for (TobjBusDirection tobjBusDirection : tobjBusDirectionList) {
                    tobjBusDirection.setRq(dto.getDate());
                }
                busDirectionService.saveList(tobjBusDirectionList);
            }
            log.info("【线路配车信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusVehicle> tobjBusVehicleList = busVehicleService.getList();
            if (tobjBusVehicleList.size() > 0) {
                busVehicleService.deleteByDate(dto);
                for (TobjBusVehicle tobjBusVehicle : tobjBusVehicleList) {
                    tobjBusVehicle.setRq(dto.getDate());
                }
                busVehicleService.saveList(tobjBusVehicleList);
            }
            List<TobjBusLine> list = tobjBusLineList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
            if (list.size() > 0) {
                List<TobjBusDirection> directionList = new ArrayList<>();
                if (tobjBusDirectionList.size() > 0) {
                    directionList = tobjBusDirectionList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
                }
                List<TobjBusVehicle> vehicleList = new ArrayList<>();
                if (tobjBusVehicleList.size() > 0) {
                    vehicleList = tobjBusVehicleList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
                }
                List<BusLine> newBusLineList =  busLineService.update(list, directionList, vehicleList);
                if (newBusLineList.size() > 0) {
                    for (BusLine newBusLine : newBusLineList) {
                        if (newBusLine.getState() == 0) {
                            // 管理员账号线路增加
                            UserXl userXl = new UserXl();
                            userXl.setJgxlid(newBusLine.getJgxlid());
                            userXl.setUid("admin");
                            userXlService.save(userXl);
                            UserXl userXl2 = new UserXl();
                            userXl2.setJgxlid(newBusLine.getJgxlid());
                            userXl2.setUid("adminsoft");
                            userXlService.save(userXl2);
                            // 线路所属账号增加
                            List<UserXlDto> userXlDtos = userXlService.getUserYhxxList(newBusLine);
                            for (UserXlDto userXlDto : userXlDtos) {
                                if (!("admin".equals(userXlDto.getUid()) || "adminsoft".equals(userXlDto.getUid()))) {
                                    UserXl userXl3 = new UserXl();
                                    userXl3.setJgxlid(newBusLine.getJgxlid());
                                    userXl3.setUid(userXlDto.getUid());
                                    userXlService.save(userXl3);
                                }
                            }
                        } else {
                            userXlService.delete(newBusLine.getJgxlid());
                        }
                    }
                }
                busLineService.updateZjLine();
            }

            log.info("【站级首末班站点基础信息】" + "【" + formatter.format(before) + "】");
            List<TobjBusLineStation> tobjBusLineStationList = busLineStationService.getList();
            if (tobjBusLineStationList.size() > 0) {
                busLineStationService.deleteByDate(dto);
                for (TobjBusLineStation tobjBusLineStation : tobjBusLineStationList) {
                    tobjBusLineStation.setRq(dto.getDate());
                }
                busLineStationService.saveList(tobjBusLineStationList);
            }
            List<TobjBusLineStation> stationList = tobjBusLineStationList.stream().filter(item -> item.getIsremove() != null && item.getIsremove() == 0).collect(Collectors.toList());
            if (list.size() > 0) {
                busLineStationService.deleteAll();
                busLineStationService.update(stationList);
            }
            // 重复项删除
            List<BusLineStationDto> repeatList = busLineStationService.getRepeatAll();
            if (repeatList.size() > 0) {
                for (BusLineStationDto busLineStationDto : repeatList) {
                    List<BusLineStation> deleteList = busLineStationService.getByRepeat(busLineStationDto);
                    if (deleteList.size() > 0) {
                        for (int i = 1; i < deleteList.size(); i++) {
                            busLineStationService.deleteById(deleteList.get(i));
                        }
                    }
                }
            }
            // 更新特殊高峰线路时间
            List<TobjBusLineStation> firstList = busLineStationService.getFirst();
            List<TobjBusLineStation> maxList = busLineStationService.getMax();
            if (firstList.size() > 0) {
                for (TobjBusLineStation tobjBusLineStation : firstList) {
                    if (maxList.size() > 0) {
                        List<TobjBusLineStation> newList = maxList.stream().filter(item -> item.getBusline_id().equals(tobjBusLineStation.getBusline_id())).collect(Collectors.toList());
                        if (newList.size() > 0) {
                            if (!StringUtils.isEmpty(newList.get(0).getPeakendtime())) {
                                BusLineStation busLineStation1 = new BusLineStation();
                                busLineStation1.setId(tobjBusLineStation.getBusstop_id());
                                busLineStation1.setXlid(tobjBusLineStation.getBusline_id());
                                busLineStation1.setMbsj(newList.get(0).getPeakendtime());
                                busLineStationService.updateSzmbById(busLineStation1);
                            }
                        }
                    }
                }
            }
            if (maxList.size() > 0) {
                for (TobjBusLineStation tobjBusLineStation : maxList) {
                    BusLineStation busLineStation00 = new BusLineStation();
                    busLineStation00.setId(tobjBusLineStation.getBusstop_id());
                    busLineStation00.setXlid(tobjBusLineStation.getBusline_id());
                    busLineStation00.setSbsj(tobjBusLineStation.getPeakendtime());
                    busLineStation00.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
                    busLineStationService.updateMzsbById(busLineStation00);
                }
            }
            // 更新首末站名称
            busLineStationService.updateYgmc();
            // 清楚30天之前数据
            enterpriseService.deleteExpireDate(dto2);
            busLineService.deleteExpireDate(dto2);
            busDirectionService.deleteExpireDate(dto2);
            busVehicleService.deleteExpireDate(dto2);
            busLineStationService.deleteExpireDate(dto2);
        } catch (Exception e) {
            log.error("【每天1点5分定时任务执行结束,执行失败：【{}】", e.getMessage());
        } finally {
            log.info("每天1点5分定时任务执行结束");
        }
    }
}
